home *** CD-ROM | disk | FTP | other *** search
Wrap
Text File | 1995-03-16 | 10.5 KB | 172 lines | [ TEXT/MMCC]
//------------------------------------------------------------------------------ // File: apple event.h // Date: 7/19/94 // Author: Bretton Wade // // Description: this file contains declarations related to Apple Event // processing // //------------------------------------------------------------------------------ #include "menu.h" #include "apple event.h" //------------------------------------------------------------------------------ // types //------------------------------------------------------------------------------ typedef pascal OSErr (*FileAEProc) (FSSpec &spec); // proc type for file AEs //------------------------------------------------------------------------------ // Check that all parameters have been retrieved from an event //------------------------------------------------------------------------------ OSErr CheckGotRequiredParams (const AppleEvent *AE) // be sure that I got all of the descriptors { // begin DescType retType; // type of attribute returned Size size; // how big it really is OSErr myErr = AEGetAttributePtr (AE, keyMissedKeywordAttr, // get an attribute pointer typeWildCard, &retType, NULL, 0, &size); // etc. switch (myErr) // examine the return value { // begin case errAEDescNotFound: // no more descriptors return noErr; // no problem case noErr: // found something return errAEParamMissed; // I missed something default: // anything else return myErr; // problem } // end } // end //------------------------------------------------------------------------------ // Extract files from apple event and call the appropriate proc //------------------------------------------------------------------------------ pascal OSErr FileAE (const AppleEvent *AE, FileAEProc proc) // handle file AEs (print doc and open doc) { // begin AEDescList docList; // place to get the descriptor list OSErr myErr; // error code myErr = AEGetParamDesc (AE, keyDirectObject, typeAEList, &docList); // get the list of file descriptors if (myErr) return myErr; // bail out if any error myErr = CheckGotRequiredParams (AE); // make sure I got everything if (!myErr) // if there was no error { // begin long count; // how many items in the file list AECountItems (&docList, &count); // see how many there are for (long i = 1; i <= count; i++) // do for all the items { // begin Size size; // returned size AEKeyword keywd; // keyword DescType retType; // returned type FSSpec mySpec; // FSSpec for target file myErr = AEGetNthPtr (&docList, i, typeFSS, &keywd, &retType, // get the file description from the apple event (Ptr) &mySpec, sizeof (FSSpec), &size); // get a file spec if (myErr) break; // bail out if any error myErr = (proc) (mySpec); // call the file proc with the spec if (myErr) break; // bail out if any error } // end } // end AEDisposeDesc (&docList); // dispose the file list return myErr; // return my error code } // end //------------------------------------------------------------------------------ // Event handler for start application event //------------------------------------------------------------------------------ pascal OSErr StartAE (const AppleEvent*, AppleEvent*, long) // start application event { // begin /* Message ("Graphics Demonstration Application"EOL); Message ("Copyright (C) 1994 Bretton Wade"EOL); Message ("bw16@cornell.edu"EOL); Message ("Cornell University"EOL); Message ("Program of Computer Graphics"EOL); Message ("All Rights Reserved"EOL EOL); */ return noErr; // no problem } // end //------------------------------------------------------------------------------ // Individual file proc for handling open document event //------------------------------------------------------------------------------ pascal OSErr OpenProc (FSSpec&) // file proc for opening documents { // begin return noErr; // no problem } // end //------------------------------------------------------------------------------ // Event handler for open document event //------------------------------------------------------------------------------ pascal OSErr OpenAE (const AppleEvent *AE, AppleEvent*, long) // open document event { // begin return FileAE (AE, OpenProc); // return my result code } // end //------------------------------------------------------------------------------ // Individual file proc for handling print document event //------------------------------------------------------------------------------ pascal OSErr PrintProc (FSSpec&) // file proc for printing documents { // begin return noErr; // no problem } // end //------------------------------------------------------------------------------ // Event handler for print document event //------------------------------------------------------------------------------ pascal OSErr PrintAE (const AppleEvent *AE, AppleEvent*, long) // print document event { // begin return FileAE (AE, PrintProc); // return my result code } // end //------------------------------------------------------------------------------ // Event Handler for quit event //------------------------------------------------------------------------------ pascal OSErr QuitAE (const AppleEvent*, AppleEvent*, long) // quit application event { // begin SendToMyself (kFileMenu, kQuitItemEvent); // send myself a quit message return noErr; // no problem } // end //------------------------------------------------------------------------------ // Install Handlers for My apple events //------------------------------------------------------------------------------ void InstallAppleEvents (void) // install my apple event handlers { // begin AEInstallEventHandler ('aevt', 'oapp', NewAEEventHandlerProc(StartAE), 0, FALSE);// open application AEInstallEventHandler ('aevt', 'odoc', NewAEEventHandlerProc(OpenAE), 0, FALSE); // open document AEInstallEventHandler ('aevt', 'pdoc', NewAEEventHandlerProc(PrintAE), 0, FALSE);// print document AEInstallEventHandler ('aevt', 'quit', NewAEEventHandlerProc(QuitAE), 0, FALSE); // quit application } // end //------------------------------------------------------------------------------ // Send Apple Event To Myself //------------------------------------------------------------------------------ void SendToMyself (AEEventClass AEclass, AEEventID AEmessage) // create an apple event and send it to myself // create and send a quit message to myself { // begin OSErr myErr; // error condition ProcessSerialNumber myPSN; // serial number of this process AEAddressDesc myAddress; // address descriptor AppleEvent myAppleEvent; // the apple event to send myErr = GetCurrentProcess (&myPSN); // get the serial number for this process if (myErr == noErr) // if there was no error myErr = AECreateDesc ( typeProcessSerialNumber, // create the target description with a process serial number (Ptr) &myPSN, // at the PSN for this process sizeof (myPSN), // the size of a PSN in bytes (AEDesc *) &myAddress); // at my address description if (myErr == noErr) // if there was no error myErr = AECreateAppleEvent ( AEclass, // create the apple event with the event class AEmessage, // the message &myAddress, // the target address for this application kAutoGenerateReturnID, // I don't care what my return ID is kAnyTransactionID, // this is not part of a transaction &myAppleEvent); // at my Apple Event if (myErr == noErr) // if there was no error myErr = AESend ( &myAppleEvent, // send my apple event NULL, // with no reply expected kAENoReply + kAECanInteract, // no reply, but you can ask the user a questio if you need to kAENormalPriority, // this is a normal message kNoTimeOut, // I won't be waiting for it to finish NULL, // no idle function NULL); // no filter function AEDisposeDesc (&myAddress); // clean up when finished } // end //------------------------------------------------------------------------------